const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const request = function(url, data, method = 'get', loading = true, loadingText = '加载中...') {
  let newData = Object.assign(data, wx.getStorageSync("login"))
  if (loading) {
    wx.showLoading({
      title: loadingText,
    })
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      method: method,
      header: method == "get" ? {
        'content-type': "application/application/json"
      } : {
        'content-type': "application/x-www-form-urlencoded"
      },
      data: newData,
      success: res => {
        resolve(res)
      },
      fail: res => {
        reject(res)
      },
      complete: res => {
        if (loading) {
          wx.hideLoading()
        }
      }
    })
  })
}
// 调用接口wx.login() 获取临时登录凭证（code）
// const login = () => {
//   return new Promise((resolve, reject) => {
//     wx.login({
//       success: function (res) {
//         if (res.code) {
//           resolve(res)
//         } else {
//           console.log('登录失败！' + res.errMsg)
//           reject(res)
//         }
//       },
//       fail: function (err) {
//         reject(err)
//       }
//     })

//   })
// }
module.exports = {
  formatTime: formatTime,
  request: request,
  // login:login
}